package com.swpu.service_weight.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import com.swpu.service_weight.entity.WeightPlan;
import com.swpu.service_weight.service.WeightPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/service_weight/weight-plan")
public class WeightPlanController {
    @Autowired
    private WeightPlanService weightPlanService;
    @PostMapping("updateUserWeightPlan")
    @Decrypt
    @Encrypt
    public R updateUserWeightPlan(@RequestBody ResultEncryption resultEncryption,  WeightPlan weightPlan,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        weightPlanService.updateUserWeightPlan(openid,weightPlan);
        return R.ok();
    }
    @GetMapping("/getUserWeightPlan")
    public R getUserWeightPlan(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        WeightPlan weightPlan = weightPlanService.getUserWeightPlan(openid);
        return R.ok().data("weightPlan",weightPlan);
    }
}

